package com.tonann.app.service.impl;

import java.util.List;

import com.tonann.app.utils.GatewayUtils;
import com.tonann.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tonann.app.mapper.AppGatewayMapper;
import com.tonann.app.domain.AppGateway;
import com.tonann.app.service.IAppGatewayService;

import javax.annotation.Resource;

/**
 * 运行网关Service业务层处理
 *
 * @author kunchan
 * @date 2022-10-09
 */
@Service
public class AppGatewayServiceImpl implements IAppGatewayService
{
    @Resource
    private AppGatewayMapper appGatewayMapper;

    /**
     * 查询运行网关
     *
     * @param gatewayId 运行网关主键
     * @return 运行网关
     */
    @Override
    public AppGateway selectAppGatewayByGatewayId(Long gatewayId)
    {
        AppGateway appGateway = GatewayUtils.getDeviceByGatewayId(gatewayId);
        if (appGateway == null) {
            appGateway = appGatewayMapper.selectAppGatewayByGatewayId(gatewayId);
            GatewayUtils.setDeviceByGatewayId(appGateway);
        }
        return appGateway;
    }

    /**
     * 查询运行网关列表
     *
     * @param appGateway 运行网关
     * @return 运行网关
     */
    @Override
    public List<AppGateway> selectAppGatewayList(AppGateway appGateway)
    {
        return appGatewayMapper.selectAppGatewayList(appGateway);
    }

    @Override
    public List<AppGateway> getAll(Long gatewayId) {
        return appGatewayMapper.selectAppGatewayListExclude(gatewayId);
    }

    /**
     * 新增运行网关
     *
     * @param appGateway 运行网关
     * @return 结果
     */
    @Override
    public int insertAppGateway(AppGateway appGateway)
    {
        appGateway.setCreateTime(DateUtils.getNowDate());
        return appGatewayMapper.insertAppGateway(appGateway);
    }

    /**
     * 修改运行网关
     *
     * @param appGateway 运行网关
     * @return 结果
     */
    @Override
    public int updateAppGateway(AppGateway appGateway)
    {
        appGateway.setUpdateTime(DateUtils.getNowDate());
        GatewayUtils.setDeviceByGatewayId(appGateway);
        return appGatewayMapper.updateAppGateway(appGateway);
    }

    /**
     * 批量删除运行网关
     *
     * @param gatewayIds 需要删除的运行网关主键
     * @return 结果
     */
    @Override
    public int deleteAppGatewayByGatewayIds(Long[] gatewayIds)
    {
        GatewayUtils.clearDeviceCache();
        return appGatewayMapper.deleteAppGatewayByGatewayIds(gatewayIds);
    }

    /**
     * 删除运行网关信息
     *
     * @param gatewayId 运行网关主键
     * @return 结果
     */
    @Override
    public int deleteAppGatewayByGatewayId(Long gatewayId)
    {
        GatewayUtils.removeDeviceCache(gatewayId.toString());
        return appGatewayMapper.deleteAppGatewayByGatewayId(gatewayId);
    }

    @Override
    public List<AppGateway> selectAppGatewayByGatewayName(String gatewayName) {
        AppGateway appGateway = new AppGateway();
        appGateway.setGatewayName(gatewayName.trim());
        return appGatewayMapper.selectAppGatewayList(appGateway);
    }
}
